package com.xxx.mch.service.impl;

import com.xxx.mch.dao.SystemConfigMapper;
import com.xxx.mch.domain.po.SystemConfig;
import com.xxx.mch.service.SystemConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author FengHaixin
 * @description 一句话描述该文件的用途
 * @date 2020/3/1
 */
@Slf4j
@Service
public class SystemConfigServiceImpl implements SystemConfigService
{
    @Autowired
    private SystemConfigMapper systemConfigMapper;


    @Override
    public List<SystemConfig> queryConfigByType(String type)
    {
        SystemConfig systemConfig = new SystemConfig();
        systemConfig.setMenu(type);
        return systemConfigMapper.select(systemConfig);
    }


    @Override
    public Map<String, SystemConfig> queryConfigByMenu(String type)
    {
        List<SystemConfig> systemConfigs = this.queryConfigByType(type);
        if (CollectionUtils.isEmpty(systemConfigs))
        {
            return new HashMap<>();
        }
        return systemConfigs.stream().collect(Collectors.toMap(SystemConfig::getType, Function.identity()));
    }


    @Override
    public void updateConfig(List<SystemConfig> systemConfigList)
    {
        if (CollectionUtils.isEmpty(systemConfigList))
        {
            log.info("待更新记录为空");
            return;
        }

        systemConfigList.forEach(systemConfig -> {
            int updated = systemConfigMapper.updateByPrimaryKeySelective(systemConfig);
            log.info("积分或权重信息更新完成，更新了{}条", updated);
        });
    }
}
